package math;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

public class MatrixUtil{
	public static FloatBuffer createPerspectiveMatrix(float frustumScale, float zNear,float zFar){
		FloatBuffer matrix= BufferUtils.createFloatBuffer(16);
		matrix.put(0, frustumScale);
		matrix.put(5, frustumScale);
		matrix.put(10, (zNear+zFar)/(zNear-zFar));
		matrix.put(14, (2*zNear*zFar)/(zNear-zFar));
		matrix.put(11, -1);
		return matrix;
	}

	public static Matrix4 createcameraToClipMatrix(float frustumScale, float zNear,float zFar){
		Matrix4 matrix= new Matrix4();
		matrix.put(0, frustumScale);
		matrix.put(5, frustumScale);
		matrix.put(10, (zNear+zFar)/(zNear-zFar));
		matrix.put(14, (2*zNear*zFar)/(zNear-zFar));
		matrix.put(11, -1);
		return matrix;
	}

	private static FloatBuffer direct16 = BufferUtils.createFloatBuffer(16);

	public static FloatBuffer toBuffer(Matrix4 m) {
		return toBuffer(direct16, m.getMatrix());
	}

	private static FloatBuffer direct4 = BufferUtils.createFloatBuffer(4);
	public static FloatBuffer toBuffer(Vector4f v) {
		return toBuffer(direct4, v.toArray());
	}
	
	private static FloatBuffer toBuffer(FloatBuffer direct, float[] flatMatrix) {
		// FloatBuffer direct = BufferUtils.createFloatBuffer(16);
		direct.clear();
		direct.put(flatMatrix);
		direct.flip();
		return direct;
	}
}
